package com.shawn.example.mvvmdemo.login.ui

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.jetpack.mvvm.lib.base.BaseActivity
import com.jetpack.mvvm.lib.base.RouterConstant
import com.jetpack.mvvm.lib.base.bus.BusKey
import com.jetpack.mvvm.lib.base.bus.LiveDataBus
import com.jetpack.mvvm.lib.binding.ext.viewBind
import com.jetpack.mvvm.lib.common.ext.observeState
import com.jetpack.mvvm.lib.common.ext.observeStateForever
import com.shawn.apt.demo.print.PrintField
import com.shawn.example.mvvmdemo.login.R
import com.shawn.example.mvvmdemo.login.databinding.ActivityLoginBinding
import com.shawn.example.mvvmdemo.login.viewmodule.LoginRegisterVM
import com.shawn.example.mvvmdemo.login.vo.UserInfoVO
import dagger.hilt.android.AndroidEntryPoint

@Route(path = RouterConstant.LoginRegister.PAGE_LOGIN)
@AndroidEntryPoint
class LoginActivity : BaseActivity(), View.OnClickListener {
    companion object {
        private const val TAG = "LoginActivity"
    }
    private val binding: ActivityLoginBinding by viewBind()
    private val vm by viewModels<LoginRegisterVM>()

    @Autowired
    @JvmField
    var routerPath: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        initView()
        observerData()
        lifecycleScope
    }

    private fun observerData() {
//        vm.userInfoLiveData.observe(this) {
//            Log.e("LoginActivity", "${it?.nickName}")
//        }
        vm.userInfoLiveData.observeState(this, UserInfoVO::nickName) {
            Toast.makeText(this, "$it", Toast.LENGTH_SHORT).show()
        }
    }

    private fun initView() {
        supportActionBar?.hide()
//        binding.actionBar.apply {
//            tvTitle.text = getString(R.string.login)
//            setSupportActionBar(binding.actionBar.toolbar)
//            supportActionBar?.setDisplayHomeAsUpEnabled(true)
//        }
        binding.btnLogin.setOnClickListener(this)
        binding.tvSignUp.setOnClickListener(this)
        binding.accountPwdView.etUsername.setText("shawnXu")
        binding.accountPwdView.etPassword.setText("123123")
    }

    override fun onClick(view: View) {
        when (view.id) {
            binding.btnLogin.id -> {
                vm.login(binding.accountPwdView.etUsername.text.toString(), binding.accountPwdView.etPassword.text.toString())
            }
            binding.tvSignUp.id -> {

            }
            else -> {}
        }
    }
}